const express = require('express')
const app = express()

app.get('/', (req, res) => res.send('Hello World!'))

app.get('/ok', (req, res) => res.json({
    id: 1,
    name: 'ok'
}))

app.get('/timeout', (req, res) => {

    let interval = Math.random() * 3000

    console.log(`[${interval < 1000}]timeout interval: ${interval}`);

    setTimeout(() => {
        res.json({
            id: 1,
            name: 'timeout'
        })
    }, interval);
})

app.get('/error', (req, res) => {

    let rate = Math.random() * 3000

    console.log(`[${rate < 1000}]error rete:${rate}`);

    if (rate < 1000) {
        res.json({
            id: 1,
            name: 'error'
        })
    } else {
        res.status(503).end();
    }


})

app.listen(3000, () => console.log('Example app listening on port 3000!'))